package main

import (
	"net/http"
)

func main() {
	http.HandleFunc("/v2/api-docs", apiDocsHandler)
	http.ListenAndServe(":8080", nil)
}

func apiDocsHandler(w http.ResponseWriter, r *http.Request) {
	// 设置允许的跨域请求头
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
	w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

	// 设置接口返回内容类型
	w.Header().Set("Content-Type", "application/json")

	// JSON 字符串
	jsonStr := `{"swagger":"2.0","info":{"description":"Spring Boot 集成 Swagger 测试接口文档","version":"v2.0","title":"Swagger 测试接口文档","termsOfService":"https://xxx.github.io","contact":{"name":"xxx","url":"https://xxx.github.io","email":"xxx@qq.com"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0"}},"host":"localhost:8080","basePath":"/","tags":[{"name":"用户模块","description":"Swagger Demo Controller"}],"paths":{"/user":{"post":{"tags":["用户模块"],"summary":"添加用户","operationId":"addUserUsingPOST","consumes":["application/json"],"produces":["*/*"],"parameters":[{"in":"body","name":"user","description":"user","required":true,"schema":{"$ref":"#/definitions/用户实体类"}}],"responses":{"200":{"description":"OK","schema":{"type":"integer","format":"int64"}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}},"deprecated":false},"put":{"tags":["用户模块"],"summary":"更新用户","operationId":"updateUserUsingPUT","consumes":["application/json"],"produces":["*/*"],"parameters":[{"in":"body","name":"user","description":"user","required":true,"schema":{"$ref":"#/definitions/用户实体类"}}],"responses":{"200":{"description":"OK","schema":{"type":"string"}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}},"deprecated":false}},"/user/{userId}":{"get":{"tags":["用户模块"],"summary":"根据用户ID查询用户信息","operationId":"queryUserByIdUsingGET","produces":["*/*"],"parameters":[{"name":"userId","in":"path","description":"userId","required":true,"type":"integer","format":"int64"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/用户实体类"}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}},"deprecated":false},"delete":{"tags":["用户模块"],"summary":"根据用户ID删除用户","operationId":"deleteUserUsingDELETE","produces":["*/*"],"parameters":[{"name":"userId","in":"path","description":"userId","required":true,"type":"integer","format":"int64"}],"responses":{"200":{"description":"OK","schema":{"type":"string"}},"204":{"description":"No Content"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"}},"deprecated":false}}},"definitions":{"用户实体类":{"type":"object","properties":{"userId":{"type":"integer","format":"int64","description":"用户id"},"username":{"type":"string","description":"用户名"}},"title":"用户实体类","description":"用户信息描述类"}}}`

	// 将 JSON 字符串转换为字节切片
	jsonBytes := []byte(jsonStr)

	// 输出 JSON 响应
	w.Write(jsonBytes)
}

// vsCode搭建Go开发环境参考：
// https://blog.csdn.net/qq_25145759/article/details/117982335
